/**
 * Created by Demon-pro on 2015/6/28.
 */
var Air = function () {

    function update(obj) {
        if (!$.isNumeric(obj.val())) {
            validate.addInvalid(obj);
            return;
        }
        var name = obj.attr("id");
        var val = obj.val();
        validate.addValid(obj);
        $.ajax({
            url: basePath + "enthalpy/air/update/" + name,
            dataType: "json",
            type: "post",
            data: name + "=" + val,
            error: function () {
                toastr.error("连接失败,请检查网络");
            }
        })
    }

    function calculateG8AndG9() {
        Metronic.blockUI({
            boxed: true,
            message: "计算中"
        });
        $.ajax({
            url: basePath + "enthalpy/air/calculate/G8",
            type: "post",
            dataType: "json",
            success: function (data) {
                $("input[id=G8]").val(data);
                $.ajax({
                    url: basePath + "enthalpy/air/calculate/G9",
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        $("input[id=G9]").val(data);
                        Metronic.unblockUI();
                    },
                    error: function () {
                        Metronic.unblockUI();
                        toastr.error("连接失败,请检查网络");
                    }
                })
            },
            error: function () {
                Metronic.unblockUI();
                toastr.error("连接失败,请检查网络");
            }
        })
    }

    return {
        init: function () {
            $("input[id*=G]").on("blur", function () {
                update($(this))
            });

            $("#cul").on("click", function () {
                var arr = [];
                $("input[id*=G][id!=G8][id!=G9]").each(function () {
                    arr.push($(this).val());
                    if (!$.isNumeric($(this).val())) {
                        validate.addInvalid($(this))
                    }
                    else {
                        validate.addValid($(this))
                    }
                });
                if (validate.hasNotNumber(arr)) {
                    toastr.error("本页存在未填数值，无法计算");
                    return;
                }
                calculateG8AndG9();
            })
        },
        recover: function () {
            $.post(basePath + "enthalpy/air/getAir", function (data) {
                $("input").autoInject({
                    data: data,
                    mode: "id"
                }, function (obj) {
                    obj.val(getValidRecoverVal(obj.val()));
                })
            });
        }
    }
}();
